package cn.agiledata.bank.linkbank.impl.chinapay;

import java.util.Iterator;
import java.util.Map;

import chinapay.PrivateKey;
import chinapay.SecureLink;

import cn.agiledata.bank.chinapay.ChinaLinkUtil;
import cn.agiledata.bank.common.util.IOUtil;
import cn.agiledata.bank.linkbank.exception.LinkBankException;

/**
 * 中国银联电子支付通用类
 * 
 * @author 马贤波
 * 
 */
class ChinapayUtils {
	
	private ChinapayUtils() {
	}

	private static SecureLink getSecureLink(String merchantId,
			String keyFilePath) {
		keyFilePath = IOUtil.getAbsolutePath(keyFilePath);
		PrivateKey pk = new PrivateKey();
		if (pk.buildKey(merchantId, 0, keyFilePath)) {
			return new SecureLink(pk);
		}
		return null;
	}

	/**
	 * 按照银联电子支付请求数字签名的规则生成数字签名
	 * 
	 * @param params
	 *            所有参数
	 * @param privateKeyFilePath
	 *            私钥证书文件相对路径
	 * @return 数字签名
	 * @throws LinkBankException
	 *             如果无法读取证书
	 */
	public static String generateDigitalSign(Map params,
			String privateKeyFilePath) throws LinkBankException {
		String merchantId = (String) params
				.get(ChinapayNetConstant.REQUEST_MERCHANT_ID);
		/*
		 * SecureLink sl = getSecureLink(merchantId, privateKeyFilePath); if (sl ==
		 * null) { throw LinkBankException.CANNOT_LOAD_CERT; }
		 */
		String orderId = (String) params
				.get(ChinapayNetConstant.REQUEST_ORDER_ID);
		String amountValue = (String) params
				.get(ChinapayNetConstant.REQUEST_AMOUNT);
		/*String currencyType = (String) params
				.get(ChinapayNetConstant.REQUEST_CURRENCY_TYPE);
		String date = (String) params.get(ChinapayNetConstant.REQUEST_DATE);
		String transType = (String) params
				.get(ChinapayNetConstant.REQUEST_TRANS_TYPE);*/
		String key = (String) params.get(ChinapayNetConstant.KEY);
		String gateId = (String) params.get(ChinapayNetConstant.BANK_GATE_ID);
		String retUrl = (String) params
				.get(ChinapayNetConstant.MERCHANT_URL_PAGE);
		System.out.println("***********************");
		System.out.println(orderId);
		System.out.println(amountValue);
		System.out.println("***********************");
		return ChinaLinkUtil.signMac(orderId, gateId, amountValue, retUrl,
				null, key);
	}

	/**
	 * 按照银联电子支付响应数字签名的规则校验数字签名
	 * 
	 * @param params
	 *            所有参数
	 * @param digitalSign
	 *            待校验的数字签名
	 * @param publicKeyFilePath
	 *            银联电子支付公钥证书文件相对路径
	 * @return true if 数字签名校验通过, otherwise false
	 * @throws LinkBankException
	 *             如果无法读取证书
	 */
	public static boolean validateDigitalSign(Map params, String digitalSign,
			String publicKeyFilePath) throws LinkBankException {
		//SecureLink sl = getSecureLink("999999999999999", publicKeyFilePath);
		/*if (sl == null) {
			throw LinkBankException.CANNOT_LOAD_CERT;
		}*/
		String merchantId = (String) params
				.get(ChinapayNetConstant.RESPONSE_MERCHANT_ID);
		String orderId = (String) params
				.get(ChinapayNetConstant.RESPONSE_ORDER_ID);
		String amountValue = (String) params
				.get(ChinapayNetConstant.RESPONSE_AMOUNT);
		/*String currencyType = (String) params
				.get(ChinapayNetConstant.RESPONSE_CURRENCY_TYPE);
		String date = (String) params.get(ChinapayNetConstant.RESPONSE_DATE);
		String transType = (String) params
				.get(ChinapayNetConstant.RESPONSE_TRANS_TYPE);
		String transStatus = (String) params
				.get(ChinapayNetConstant.TRANS_STATUS);*/
		String gateId=(String)params.get(ChinapayNetConstant.BANK_GATE_ID);
		String code=(String)params.get(ChinapayNetConstant.TRANS_STATUS);
		String ext1=(String)params.get(ChinapayNetConstant.EXT);
		String sign=(String)params.get(ChinapayNetConstant.RESPONSE_DIGITAL_SIGN);
		String key="JDEyW8iGw";
		return ChinaLinkUtil.verifyTransResponse(orderId, gateId, amountValue, code, ext1, key,sign);
	}
}
